package logic

import (
	"anban/models"
	"anban/service"
	"anban/utils"
	"anban/utils/wechat"
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"net/url"
)

func AddGrade(input url.Values) *utils.Error {
	if len(input["user_id"][0]) <= 0 {
		return utils.NewError(0, "请选择发布人")
	}
	if len(input["student_id"][0]) <= 0 {
		return utils.NewError(0, "请选择学生")
	}
	if len(input["test_name"][0]) <= 0 {
		return utils.NewError(0, "请输入考试名称")
	}
	if len(input["chinese_grade"][0]) <= 0 {
		return utils.NewError(0, "请输入语文成绩")
	}
	if len(input["mathematics_grade"][0]) <= 0 {
		return utils.NewError(0, "请输入数学成绩")
	}
	if len(input["english_grade"][0]) <= 0 {
		return utils.NewError(0, "请输入英语成绩")
	}

	_, err := service.AddGradeNotice(input)
	if err != nil {
		return utils.NewError(0, "发布失败")
	}

	input = url.Values{
		"student_id": {input["student_id"][0]},
		"type": {"5"},
	}
	service.AddNewNotice(input)

	return nil
}

func AddGradeFromExcel(input url.Values) *utils.Error {
	if len(input["user_id"][0]) <= 0 {
		return utils.NewError(0, "请选择发布人")
	}
	if len(input["test_name"][0]) <= 0 {
		return utils.NewError(0, "请输入考试名称")
	}
	if len(input["excel_file_path"][0]) <= 0 {
		return utils.NewError(0, "请上传excel文件")
	}

	f, err := excelize.OpenFile("." + input["excel_file_path"][0])
	if err != nil {
		return utils.NewError(0, "excel文件无法打开，请重新上传")
	}

	o := orm.NewOrm()
	var p map[string]interface{}
	var gradeNotices []models.GradeNotice
	var newNotices []models.NewNotice

	user := &models.User{}
	user.Id = utils.Atoi64(input["user_id"][0])
	o.Read(user)

	rows := f.GetRows("Sheet1")
	for k, row := range rows {
		if k == 0 {
			continue
		}

		p = map[string]interface{}{}
		p["idCard"] = row[1]
		student := service.GetStudentInfo(p)
		if student.Id <= 0 {
			return utils.NewError(0, fmt.Sprintf("%s的身份证号不正确，无法导入", row[0]))
		}

		newNotice := models.NewNotice{}
		newNotice.User = user
		newNotice.Type = 5
		newNotice.Student = student
		newNotices = append(newNotices, newNotice)

		gradeNotice := models.GradeNotice{}
		gradeNotice.User = user
		gradeNotice.Student = student
		gradeNotice.TestName = input["test_name"][0]
		gradeNotice.ChineseGrade = utils.Atof64(row[2])
		gradeNotice.MathematicsGrade = utils.Atof64(row[3])
		gradeNotice.EnglishGrade = utils.Atof64(row[4])
		if len(row) > 5 {
			gradeNotice.ChemistryGrade = utils.Atof64(row[5])
		}
		if len(row) > 6 {
			gradeNotice.PhysicsGrade = utils.Atof64(row[5])
		}
		if len(row) > 7 {
			gradeNotice.GeographyGrade = utils.Atof64(row[5])
		}
		if len(row) > 8 {
			gradeNotice.HistoryGrade = utils.Atof64(row[5])
		}
		if len(row) > 9 {
			gradeNotice.BiologyGrade = utils.Atof64(row[5])
		}
		if len(row) > 10 {
			gradeNotice.PoliticsGrade = utils.Atof64(row[5])
		}
		gradeNotices = append(gradeNotices, gradeNotice)
	}

	_, err = o.InsertMulti(1, gradeNotices)
	if err != nil {
		return utils.NewError(0, "导入失败")
	}

	o.InsertMulti(1, newNotices)

	go SendGradeNoticeFromExcel(input)

	return nil
}

func SendGradeNoticeFromExcel(input url.Values) {
	f, _ := excelize.OpenFile("." + input["excel_file_path"][0])

	var p map[string]interface{}

	domain := beego.AppConfig.String("domain")
	path := beego.URLFor("parent.GradeNoticeController.ShowList")
	wechatUrl := fmt.Sprintf("%s%s", domain, path)

	rows := f.GetRows("Sheet1")
	for k, row := range rows {
		if k == 0 {
			continue
		}

		p = map[string]interface{}{}
		p["idCard"] = row[1]
		student := service.GetStudentInfo(p)

		if student.Id > 0 && len(student.FamilySn) > 0 && student.Status == 1 {
			p = map[string]interface{}{}
			p["status"] = 1
			p["familySn"] = student.FamilySn
			_, userList := service.GetUserList(p)
			for _, user := range userList {
				if user.SubscribeStatus == 1 {
					wechat.SendGradeNotice(user.OpenId, "您有新的成绩通知", "", wechatUrl, row[0], input["test_name"][0], "点击查看孩子成绩")
				}
			}
		}
	}
	return
}